package br.phb.cgame;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import br.phb.cgame.sprites.BasicSprite;

public class MainActivity extends Activity {

	private BasicSprite sprite;
	private MyThread thread;

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_main );
		sprite = new BasicSprite( this, R.drawable.sprite, 4, 4, 10, 20 );
		this.sprite.moviments.put( "down", new int[] { 0, 1, 2, 3 } );
		this.sprite.moviments.put( "left", new int[] { 4, 5, 6, 7 } );
		this.sprite.moviments.put( "right", new int[] { 8, 9, 10, 11 } );
		this.sprite.moviments.put( "up", new int[] { 12, 13, 14, 15 } );
		setContentView( sprite.getView() );
		thread = new MyThread();
		sprite.getView().setOnClickListener( new OnClickListener() {

			@Override
			public void onClick( View arg0 ) {
				thread.isRun = !thread.isRun;
				thread.start();
			}
		} );
	}

	class MyThread extends Thread {
		boolean isRun = true;

		@Override
		public void run() {
			while ( isRun ) {
				sprite.animate( "up" );
				try {
					sleep( 1000 );
				} catch ( InterruptedException e ) {
					e.printStackTrace();
				}
			}
		}
	}
}
